import zipfile
from tqdm import tqdm
import itertools


def rar_attack(file_name):
    file_handle = zipfile.RarFile(file_name)
    for length in range(1, 9):
        # 思路是这样，但实际运行过程中，会消耗大量资源，耗时较长
        # for combination in tqdm(itertools.product('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()', repeat=length)):
        for combination in tqdm(itertools.product('0123456789', repeat=length)):
            password = ''.join(combination)
            try:
                file_handle.extractall(pwd=password)
                print('Password found: ', password)
                return
            except:
                pass


if __name__ == '__main__':
    # 压缩包，需要和代码同级
    file_name = '123.zip'
    rar_attack(file_name)
